package GUI;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JTextField;

import client.ClientConsole;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;

/**this class get book to delete from db*/
public class GUI_DeleteBook extends JPanel
{
	/**clc instance of ClientConsole*/
	private ClientConsole clc;
	/**list ArrayList of results from server*/
	private ArrayList<String[]> list;
	/**table JTable to display results*/
	private JTable table;
	/**textField to get book id to remove from db*/
	private JTextField textField;
	
	/**@param cc current ClientConsole
	 * @param gl instance of current GUI_LogIn
	 * @param gc instance of current GUI_Catalog*/
	public GUI_DeleteBook(ClientConsole cc,final GUI_LogIn gl,final GUI_Catalog gc) {
		clc=cc;
		this.setSize(700, 600);
		setLayout(null);
		
		JLabel lblChooseBookTo = new JLabel("Choose book to remove from catalog:");
		lblChooseBookTo.setBounds(39, 30, 413, 29);
		add(lblChooseBookTo);
		
		textField = new JTextField();
		textField.setBounds(254, 364, 86, 20);
		add(textField);
		textField.setColumns(10);
		
		showBooks();
		JButton btnDeleteBook = new JButton("Delete Book");
		btnDeleteBook.setBounds(365, 363, 116, 23);
		btnDeleteBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
						String s[]={"deleteBook",textField.getText()};
						clc.client.sendToServer(s);
						while(clc.client.flag==0)
							System.out.print("");;
						clc.client.flag=0;
						JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
						showBooks();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		add(btnDeleteBook);
		
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(473, 424, 89, 23);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(gc);
			}
		});
		add(btnBack);
		
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(572, 424, 89, 23);
		add(btnLogOut);
		
		JLabel lblEnterBookId = new JLabel("Enter book ID to delete:");
		lblEnterBookId.setBounds(32, 367, 214, 14);
		add(lblEnterBookId);
		
		this.setVisible(true);
	}
	
	/**this method display books and their details*/
	public void showBooks()
	{
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(39, 93, 461, 236);
		add(scrollPane);
		try {
			String s[]={"getBooks"};
			clc.client.sendToServer(s);
			while(clc.client.flag==0)
				System.out.print("");;
			clc.client.flag=0;
			list=clc.client.details;
			table = new JTable(list.size(),3);
			table.getColumnModel().getColumn(0).setHeaderValue("Book ID");
			table.getColumnModel().getColumn(1).setHeaderValue("Name");
			table.getColumnModel().getColumn(2).setHeaderValue("Catalog Number");
			scrollPane.setViewportView(table);
			
			for(int i=0;i<list.size();i++)
			{
				for(int j=0;j<3;j++)
				{
					table.setValueAt(list.get(i)[j],i,j);
				}
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}
